﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.dao;
using Peekaboo2008.common;
using Peekaboo2008.model;
using Peekaboo2008.controls;
using System.Collections;
using System.Data.SqlClient;
namespace Peekaboo2008.view
{
    public partial class FormXuatBanHangHoaByBarcode : Form
    {
        KhachHangModel khachHang = null;
        DataTable tbDanhSachHangHoa;

        //Luu id cua phieu nhap xuat da insert
        PhieuNhapXuatModel _PhieuNhap = null;

        //List luu tat ca cac truong gia tri da insert trong chi tiet nhap xuat
        //bao gom so luong va ma hang, dung de revert lai khi qua trinh insert du lieu gap van de
        List<ChiTietNhapXuatModel> ListChiTietNhapXuatModel = new List<ChiTietNhapXuatModel>();

        //Kich hoat khi ng dung da luu hoa don thanh cong
        private Boolean IsSaved = false;

        NgoaiTeModel NgoaiTeModel;

        //Bien luu cac truong gia tri se nhap vao phieu nhap xuat
        private String MaHoaDon = "";
        private double TiGia = 1;

        //Tong tien hang trong danh sach chua thue va ck
        private double CongTienHang = 0;

        //2 gia tri ben duoi luu tong VAT va CK cua tung mon hang
        private double TienVATHang = 0;

        //VAT nguoi dung nhap
        private double VAT = 0;
        private double TienVAT = 0;

        //Chiet khau dua tren loai khach hang
        private double ChietKhau = 0;
        private double ChietKhauVND = 0;

        private double TongTien = 0;
        private double ThanhToan = 0;
        private double ThanhToanVND = 0;
        private double ConNo = 0;
        private double ConNoVND = 0;
        public FormXuatBanHangHoaByBarcode()
        {
            InitializeComponent();
            initHinhThucThanhToan();
            initMaHoaDon();
            initNgayNhapHoaDon();
            initTienTe();
            initKhachHang(KhachHangDAO.KhachHangMuaHang_GetByMaKhuVuc(GetChiNhanh().MaKhuVuc));
            initDanhSachHangHoa();
        }
        /*Khoi tạo txt ngay nhap hoa don*/
        private void initNgayNhapHoaDon()
        {
            txtNgayTaoHoaDon.Text = CommonDAO.getDate();
        }

        /**/
        private void initMaHoaDon()
        {
            String[] _s = CommonDAO.getDate().Split('-');
            MaHoaDon = "HDX-B" + _s[2] + _s[1] + _s[0] + "-*****";
            txtMaHoaDon.Text = MaHoaDon.ToString();
        }

        /*Khoi tạo loai tien thanh toan*/
        private void initTienTe()
        {
            DataTable _tbTienTe = NgoaiTeDAO.getAll();
            cbxLoaiTien.DataSource = _tbTienTe;
            cbxLoaiTien.DisplayMember = "Don_vi_goc";
            cbxLoaiTien.ValueMember = "ID";
            cbxLoaiTien.SelectedIndex = 0;
        }

        /*Khoi tạo hinh thuc thanh toan*/
        private void initHinhThucThanhToan()
        {
            DataTable tb = HinhThucThanhToanDAO.getAll();
            cbxHinhThucThanhToan.DataSource = HinhThucThanhToanDAO.getAll();
            cbxHinhThucThanhToan.DisplayMember = "Ten";
            cbxHinhThucThanhToan.ValueMember = "ID";
        }
        /*Khoi tao datatable luu danh sach hang hoa khi mua*/
        private void initDanhSachHangHoa()
        {
            tbDanhSachHangHoa = new DataTable("DanhSachHangHoa");

            tbDanhSachHangHoa.Columns.Add("Ma_hang_hoa", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Ten_hang_hoa", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Barcode", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Ten_don_vi_qui_doi", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Ma_don_vi_qui_doi", typeof(String));
            tbDanhSachHangHoa.Columns.Add("So_luong", typeof(String));
            tbDanhSachHangHoa.Columns.Add("So_luong_ton", typeof(String));
            tbDanhSachHangHoa.Columns.Add("VAT", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Tien_VAT", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Chiet_khau", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Tien_chiet_khau", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Gia_xuat_cuoi", typeof(String));
            tbDanhSachHangHoa.Columns.Add("Thanh_tien", typeof(String));

        }
        /*lay ma khu vuc cua nhan vien hien hanh
        *khi nao moi nguoi chinh sua xong kieu du lieu sai lech trong model thi bo PARSE
        */
        private ChiNhanhModel GetChiNhanh()
        {
            return ChiNhanhDAO.getByMaChiNhanh(Constants.USER_LOGIN.MaChiNhanh);
        }

        /**Load danh sach khach hang*/
        public void initKhachHang(DataTable _tbKhachHang)
        {
            int wID = 0;
            int wTen = 300;
            int wTel = 100;
            int wDiaChi = cbxKhachHang.Width - (wID + wTen + wTel);
            wDiaChi = wDiaChi < 0 ? 200 : wDiaChi;
            cbxKhachHang.Clear();
            cbxKhachHang.SourceDataString = new string[4] { "ID", "Ten", "Tel", "Dia_chi" };
            cbxKhachHang.ColumnWidth = new string[4] { wID.ToString(), wTen.ToString(),
                                                        wTel.ToString(), wDiaChi.ToString()};
            cbxKhachHang.DataSource = _tbKhachHang;
        }

        private void TinhToan()
        {
            double Num;
            bool IsNum = double.TryParse(txtThanhToan.Text.Trim(), out Num);
            if (IsNum)
            {
                ConNoVND = 0;
                TongTien = 0;

                TienVAT = CongTienHang * VAT / 100;
                ChietKhauVND = CongTienHang * ChietKhau / 100;
                ThanhToanVND = TiGia * ThanhToan;
                TongTien = CongTienHang + TienVATHang + TienVAT - ChietKhauVND;
                ConNoVND = ThanhToanVND - TongTien;
                ConNo = ConNoVND / TiGia;

                txtThanhToanVND.Text = ThanhToanVND.ToString();
                txtTyGiaVND.Text = TiGia.ToString();
                txtCongTienHang.Text = CongTienHang.ToString();
                txtTienVAT.Text = TienVAT.ToString();
                txtCK.Text = ChietKhau.ToString();
                txtCKVND.Text = ChietKhauVND.ToString();
                txtConNoVND.Text = ConNoVND.ToString();
                txtConNo.Text = ConNo.ToString();
                txtTongTien.Text = TongTien.ToString();

                enableLuuHoaDon();

            }
            else
            {
                txtThanhToan.Text = "0";
            }

        }

        private void clearForm()
        {
            TiGia = 0;
            CongTienHang = 0;
            TienVAT = 0;
            ChietKhau = 0;
            ChietKhauVND = 0;
            TongTien = 0;
            ThanhToan = 0;
            ThanhToanVND = 0;
            ConNo = 0;
            ConNoVND = 0;

            initMaHoaDon();
            initNgayNhapHoaDon();
            initTienTe();
            initHinhThucThanhToan();
            initKhachHang(KhachHangDAO.KhachHangMuaHang_GetByMaKhuVuc(GetChiNhanh().MaKhuVuc));
            tbDanhSachHangHoa.Rows.Clear();
            grvDanhSachHangHoa.DataSource = tbDanhSachHangHoa;

            txtCongTienHang.Text = "0";
            txtDienGiai.Clear();
            txtTienVAT.Text = "0";
            txtCK.Text = "0";
            txtCKVND.Text = "0";
            txtTongTien.Text = "0";
            txtThanhToan.Text = "0";
            txtThanhToanVND.Text = "0";
            txtConNo.Text = "";
            txtConNoVND.Text = "";
        }

        private void enableLuuHoaDon()
        {
            btnLuuHoaDon.Enabled = (TongTien > 0 && IsSaved == false) ? true : false;
        }

        private void enableHuyInXemInHoaDon()
        {
            btnHuyHoaDon.Enabled = IsSaved ? true : false;
            btnInHoaDon.Enabled = IsSaved ? true : false;
            btnXemIn.Enabled = IsSaved ? true : false;
        }
        /**
         * Kiem tra du lieu dua vao co phai la kieu int va nam trong gioi han cua int hay ko
         * */
        private int isNumber(Object _Input, int _DefaultVal, int _MinVal, int _MaxVal)
        {
            int _Val = _DefaultVal;
            if (_Input != null && !String.IsNullOrEmpty(_Input.ToString()))
            {
                try
                {
                    _Val = int.Parse(_Input.ToString());
                    if (_Val > _MaxVal || _Val < _MinVal)
                    {
                        _Val = _DefaultVal;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    _Val = _DefaultVal;
                }
            }
            else
            {
                _Val = _DefaultVal;
            }
            return _Val;
        }

        private void enableForm(Boolean _vl)
        {
            if (!_vl)
            {
                grbSanPham.Enabled = false;
                grbThongKeTien.Enabled = false;

                enableLuuHoaDon();
                enableHuyInXemInHoaDon();
            }
            else
            {
                grbSanPham.Enabled = true;
                grbThongKeTien.Enabled = true;

                enableLuuHoaDon();
                enableHuyInXemInHoaDon();
            }
        }
        private void searchHangInKho(String _barcode)
        {
            DataTable _tbResult = HangHoaDAO.getHangHoaTonKhoOfChiNhanhByMaChiNhanh_Barcode(Constants.USER_LOGIN.MaChiNhanh, _barcode);
            DataRow _Dr;
            if (_tbResult != null && _tbResult.Rows.Count > 0)
            {
                _Dr = tbDanhSachHangHoa.NewRow();
                _Dr["Ma_hang_hoa"] = _tbResult.Rows[0]["Ma_hang_hoa"].ToString();
                _Dr["Ten_hang_hoa"] = _tbResult.Rows[0]["Ten_hang_hoa"].ToString();
                _Dr["Barcode"] = _tbResult.Rows[0]["Barcode"].ToString();
                _Dr["Ten_don_vi_qui_doi"] = _tbResult.Rows[0]["Ten_don_vi_qui_doi"].ToString();
                _Dr["Ma_don_vi_qui_doi"] = _tbResult.Rows[0]["Ma_don_vi_qui_doi"].ToString();
                _Dr["So_luong"] = "0" ;
                _Dr["So_luong_ton"] = _tbResult.Rows[0]["So_luong_ton"].ToString();
                _Dr["VAT"] = _tbResult.Rows[0]["VAT"].ToString();
                _Dr["Tien_VAT"] = "0";
                _Dr["Chiet_khau"] = _tbResult.Rows[0]["Chiet_khau"].ToString();
                _Dr["Tien_chiet_khau"] = "0";
                _Dr["So_luong_ton"] = _tbResult.Rows[0]["So_luong_ton"].ToString();
                _Dr["Gia_xuat_cuoi"] = _tbResult.Rows[0]["Gia_xuat_cuoi"].ToString();
                _Dr["Thanh_tien"] = "0";
                tbDanhSachHangHoa.Rows.Add(_Dr);
            }
            else
            {
                
            }
            grvDanhSachHangHoa.DataSource = tbDanhSachHangHoa;

        }
        /*Tong tien dong hang trong list danh sach hang mua*/
        private void CongTienHangDanhSach()
        {
            CongTienHang = 0;
            foreach (DataGridViewRow dr in grvDanhSachHangHoa.Rows)
            {
                if ( dr.Cells["clmSoLuong"].Value !=null && 
                        !String.IsNullOrEmpty( dr.Cells["clmSoLuong"].Value.ToString()) &&
                        !dr.Cells["clmSoLuong"].Value.ToString().Equals("0"))
                {
                    CongTienHang += double.Parse(dr.Cells["clmThanhTien"].Value.ToString());
                }
            }
        }

        private Boolean validDanhSachHangHoa()
        {
            Boolean rs = false;
            if (grvDanhSachHangHoa.Rows.Count > 0)
            {
                for (int i = 0; i < grvDanhSachHangHoa.Rows.Count - 1; i++)
                {
                    if (grvDanhSachHangHoa.Rows[i].Cells["clmSoLuong"].Value == null ||
                            grvDanhSachHangHoa.Rows[i].Cells["clmSoLuong"].Value.ToString().Trim().Equals(""))
                    {
                        grvDanhSachHangHoa.Rows.RemoveAt(i);
                        continue;
                    }
                    int _SoLuong = Int32.Parse(grvDanhSachHangHoa.Rows[i].Cells["clmSoLuong"].Value.ToString().Trim());
                    int _SoLuongTon = Int32.Parse(grvDanhSachHangHoa.Rows[i].Cells["clmSoLuongTon"].Value.ToString().Trim());
                    if (_SoLuong > _SoLuongTon)
                    {
                        MessageBox.Show(this, "Số hàng trong kho không đủ\n"
                            + "Dòng: " + i, "Error", MessageBoxButtons.OK);
                        grvDanhSachHangHoa.Rows[i].Selected = true;
                        rs = true;
                        break;
                    }

                }
            }
            else
            {
                MessageBox.Show(this, "Yêu cầu nhập danh sách hàng cần xuất", "Thông báo", MessageBoxButtons.OK);
                rs = true;
            }
            return rs;
        }

        private Boolean validKhachHang()
        {
            Boolean rs = false;
            if (cbxKhachHang.SelectedItem == null || KhachHangDAO.GetByID(cbxKhachHang.SelectedItem.Value.ToString()) == null)
            {
                MessageBox.Show(this, "Khách hàng không tồn tại", "Cảnh báo", MessageBoxButtons.OK);
                rs = true;
            }
            return rs;
        }

        private void insertData()
        {
            _PhieuNhap = new PhieuNhapXuatModel();
            _PhieuNhap.KyHieu = txtMaHoaDon.Text;
            _PhieuNhap.GhiChu = "";
            _PhieuNhap.MaKhachHangNhaCungCap = cbxKhachHang.SelectedItem == null ? "" : cbxKhachHang.SelectedItem.Value.ToString();
            _PhieuNhap.MaNhanVien = Constants.USER_LOGIN.MaThanhVien;
            _PhieuNhap.TongTien = CongTienHang.ToString();
            _PhieuNhap.Thue = VAT.ToString();
            _PhieuNhap.TienThue = TienVAT.ToString();
            _PhieuNhap.ThanhTien = TongTien.ToString();
            _PhieuNhap.ThanhToan = ThanhToan.ToString();
            _PhieuNhap.ChietKhau = ChietKhau.ToString();
            _PhieuNhap.TienChietKhau = ChietKhauVND.ToString();
            _PhieuNhap.TyGia = TiGia.ToString();
            _PhieuNhap.TienNo = ConNoVND.ToString();
            _PhieuNhap.MaHinhThucThanhToan = cbxHinhThucThanhToan.SelectedValue.ToString();
            _PhieuNhap.MaNgoaiTe = cbxHinhThucThanhToan.SelectedValue.ToString();

            ChiTietNhapXuatModel _ChiTietNhapXuatModel;
            foreach (DataGridViewRow _dgr in grvDanhSachHangHoa.Rows)
            {
                _ChiTietNhapXuatModel = new ChiTietNhapXuatModel();

                _ChiTietNhapXuatModel.MaHangHoa = _dgr.Cells["clmMaHangHoa"].Value.ToString();
                _ChiTietNhapXuatModel.SoLuong = _dgr.Cells["clmSoLuong"].Value.ToString();
                _ChiTietNhapXuatModel.GiaBan = _dgr.Cells["clmDonGia"].Value.ToString();
                _ChiTietNhapXuatModel.ThanhTien = _dgr.Cells["clmThanhTien"].Value.ToString();
                _ChiTietNhapXuatModel.Thue = _dgr.Cells["clmVAT"].Value.ToString();
                _ChiTietNhapXuatModel.TienThue = _dgr.Cells["clmTienVAT"].Value.ToString();
                _ChiTietNhapXuatModel.TongTien = (float.Parse(_dgr.Cells["clmThanhTien"].Value.ToString()) +
                        float.Parse(_dgr.Cells["clmThanhTien"].Value.ToString()) * float.Parse(_dgr.Cells["clmVAT"].Value.ToString()) / 100).ToString();
                _ChiTietNhapXuatModel.ChietKhau = _dgr.Cells["clmPCK"].Value.ToString();
                _ChiTietNhapXuatModel.TienChietKhau = _dgr.Cells["clmTienCK"].Value.ToString();
                _ChiTietNhapXuatModel.HanSuDung = "";
                _ChiTietNhapXuatModel.BaoHanh = "";
                _ChiTietNhapXuatModel.PhiVanChuyen = "0";
                _ChiTietNhapXuatModel.GhiChu = "";
                _ChiTietNhapXuatModel.SoLuongGoc = _dgr.Cells["clmSoLuongTon"].Value.ToString();
                ListChiTietNhapXuatModel.Add(_ChiTietNhapXuatModel);

            }
            int _MaPhieuNhapXuat = PhieuNhapXuatDAO.Insert(_PhieuNhap, ListChiTietNhapXuatModel, Constants.USER_LOGIN.MaChiNhanh);
            if (_MaPhieuNhapXuat != -1)
            {
                _PhieuNhap = PhieuNhapXuatDAO.GetById(_MaPhieuNhapXuat.ToString());
                txtMaHoaDon.Text = _PhieuNhap.KyHieu;
                grvDanhSachHangHoa.ClearSelection();
                IsSaved = true;
                enableLuuHoaDon();
                enableHuyInXemInHoaDon();
                enableForm(false);
            }
            else
            {
                MessageBox.Show(this, "Có lỗi xẩy ra trong quá trình cập nhập dữ liệu,\nXem xét lại đường truyền hoặc restart máy tính", "Thông báo", MessageBoxButtons.OK);
            }
            
        }

        private void luuHoaDon()
        {
            if (TongTien > 0 && !validDanhSachHangHoa() && !validKhachHang())
            {
                if (ConNoVND < 0)
                {
                    DialogResult anw = MessageBox.Show(this, "Khách hàng còn nợ: " + ConNoVND.ToString() + "đ", "Cảnh báo", MessageBoxButtons.YesNo);
                    if (anw == DialogResult.Yes)
                    {
                        insertData();
                    }
                }
                else
                {
                    insertData();
                }
            }
        }

        private void huyHoaDon()
        {
            if (IsSaved)
            {
                DialogResult rs = MessageBox.Show(this, "Bạn muốn hủy hóa đơn?", "Thông báo", MessageBoxButtons.YesNo);
                if (rs == DialogResult.Yes && _PhieuNhap.ID != null && ListChiTietNhapXuatModel.Count > 0)
                {
                    if (PhieuNhapXuatDAO.DeleteByID(_PhieuNhap.ID))
                    {
                        _PhieuNhap.ID = null;
                        clearForm();
                        IsSaved = false;
                        enableHuyInXemInHoaDon();
                        enableLuuHoaDon();
                    }
                    else
                    {
                        MessageBox.Show(this, "Có sự cố xẩy ra, không thể xóa hóa đơn !", "Thông báo", MessageBoxButtons.OK);
                    }
                }
            }
        }

        private void inHoaDon()
        {
            if (IsSaved)
            {
                PhieuNhapXuatDAO.XuatBanHangHoa_InHoaDon(_PhieuNhap, ListChiTietNhapXuatModel, Constants.USER_LOGIN.MaChiNhanh);
                initDanhSachHangHoa();
                clearForm();
                IsSaved = false;
                enableHuyInXemInHoaDon();
            }
        }

        private void taoMoiHoaDon()
        {
            clearForm();
            enableForm(true);
            IsSaved = false;
            enableLuuHoaDon();
            enableHuyInXemInHoaDon();
            //cbxLoaiTien.Focus();
        }

        private bool FormKeyDown(Keys keyCode)
        {
            switch (keyCode)
            {
                case Keys.F1:
                    luuHoaDon();
                    return true;
                case Keys.F2:
                    huyHoaDon();
                    return true;
                case Keys.F3:
                    inHoaDon();
                    return true;

                case Keys.F4:

                    return true;
                case Keys.F5:
                    taoMoiHoaDon();
                    return true;

                case Keys.F6:
                    FormTimKiemPhieuHang _FormTimKiem = new FormTimKiemPhieuHang();
                    _FormTimKiem.ShowDialog();
                    return true;

                case Keys.F7:
                    this.Dispose();
                    return true;

            }
            return false;
        }

        private void txtMaVach_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                String _Barcode = txtMaVach.Text.Trim();
                searchHangInKho(_Barcode);
                txtMaVach.Text = "";
            }
            
        }

        private void grvDanhSachHangHoa_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == grvDanhSachHangHoa.Columns["clmSoLuong"].Index &&
                    grvDanhSachHangHoa.Rows[e.RowIndex].Cells["clmSoLuong"].Value != null)
            {
                Double _DonGia = 0;
                Double _VAT = 0;
                Double _TienVAT = 0;
                Double _ChietKhau = 0;
                Double _TienChietKhau = 0;
                Double _ThanhTien = 0;
                double Num;
                Double _SoLuong = 0;
                bool IsNum = double.TryParse(grvDanhSachHangHoa.Rows[e.RowIndex].Cells["clmSoLuong"].Value.ToString(), out Num);
                if (IsNum)
                {
                    _SoLuong = Double.Parse(grvDanhSachHangHoa.Rows[e.RowIndex].Cells["clmSoLuong"].Value.ToString());
                    _DonGia = Double.Parse(tbDanhSachHangHoa.Rows[e.RowIndex]["Gia_xuat_cuoi"].ToString());

                    _VAT = Double.Parse(tbDanhSachHangHoa.Rows[e.RowIndex]["VAT"].ToString());
                    _TienVAT = _SoLuong * _DonGia * _VAT / 100;

                    _ChietKhau = Double.Parse(tbDanhSachHangHoa.Rows[e.RowIndex]["Chiet_khau"].ToString());
                    _TienChietKhau = _SoLuong * _DonGia * _ChietKhau / 100;

                    _ThanhTien = _SoLuong * _DonGia + _TienVAT - _TienChietKhau;

                    
                }
                tbDanhSachHangHoa.Rows[e.RowIndex]["So_luong"] = _SoLuong;
                tbDanhSachHangHoa.Rows[e.RowIndex]["Tien_VAT"] = _TienVAT.ToString();
                tbDanhSachHangHoa.Rows[e.RowIndex]["Tien_chiet_khau"] = _TienChietKhau.ToString();
                tbDanhSachHangHoa.Rows[e.RowIndex]["Thanh_tien"] = _ThanhTien.ToString();

                grvDanhSachHangHoa.DataSource = tbDanhSachHangHoa;

                CongTienHangDanhSach();
                TinhToan();
            }
        }

        private void txtThue_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == (Keys.Control | Keys.V))
            {
                txtThue.Text = "0";
            }
            if (!String.IsNullOrEmpty(txtThue.Text.Trim()))
            {
                VAT = double.Parse(txtThue.Text.Trim());
                TinhToan();
            }
        }

        private void txtThue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtThanhToan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtThanhToan_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == (Keys.Control | Keys.V))
                txtThanhToan.Text = "0";
            if (!String.IsNullOrEmpty(txtThanhToan.Text))
            {
                ThanhToan = double.Parse(txtThanhToan.Text.Trim());
                TinhToan();
            }
        }

        private void cbxLoaiTien_SelectedValueChanged(object sender, EventArgs e)
        {
            NgoaiTeModel = NgoaiTeDAO.getById(cbxLoaiTien.SelectedValue.ToString());
            if (NgoaiTeModel != null)
            {
                TiGia = double.Parse(NgoaiTeModel.TyGia);
                TinhToan();
            }
        }

        private void txtTyGiaVND_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtMaHoaDon_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtNgayTaoHoaDon_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtCongTienHang_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtTienVAT_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtCK_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtCKVND_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtTongTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtThanhToanVND_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtConNoVND_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtConNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void cbxKhachHang_Leave(object sender, EventArgs e)
        {
            if (cbxKhachHang.SelectedItem != null)
            {
                if (!String.IsNullOrEmpty(cbxKhachHang.SelectedItem.ItemData[0].ToString()))
                {
                    String khachHangID = cbxKhachHang.SelectedItem.ItemData[0].ToString();
                    khachHang = KhachHangDAO.GetByID(khachHangID);
                    String _ck = NhomKhachHangDAO.getByID(khachHang.MaNhomKhachHang).ChietKhau;
                    ChietKhau = double.Parse(_ck);
                    TinhToan();
                }
            }
        }

        private void btnLuuHoaDon_Click(object sender, EventArgs e)
        {
            luuHoaDon();
        }

        private void btnHuyHoaDon_Click(object sender, EventArgs e)
        {
            huyHoaDon();
        }

        private void btnInHoaDon_Click(object sender, EventArgs e)
        {
            inHoaDon();
        }

        private void btnXemIn_Click(object sender, EventArgs e)
        {

        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            FormTimKiemPhieuHang _FormTimKiem = new FormTimKiemPhieuHang();
            _FormTimKiem.ShowDialog();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void tsbTaoHoaDonXuat_Click(object sender, EventArgs e)
        {
            taoMoiHoaDon();
        }

        private void FormXuatBanHangHoaByBarcode_KeyDown(object sender, KeyEventArgs e)
        {
            Control ctrl = (Control)sender;
            ctrl.Focus();
            e.Handled = FormKeyDown(e.KeyCode);
        }
    }
}
